package com.ck.controller.teacher;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.AbstractController;

import com.ck.domain.Exam;
import com.ck.domain.Panduan;
import com.ck.domain.Paper;
import com.ck.domain.Tiankong;
import com.ck.domain.Xuanze;
import com.ck.service.ExamService;
import com.ck.service.QuestionService;
import com.ck.service.TeacherService;

public class ShowPaperController extends AbstractController {
	
	private ExamService examService;
	private QuestionService questionService;
	private TeacherService teacherService;

	public void setExamService(ExamService examService) {
		this.examService = examService;
	}

	public void setQuestionService(QuestionService questionService) {
		this.questionService = questionService;
	}

	public void setTeacherService(TeacherService teacherService) {
		this.teacherService = teacherService;
	}

	@Override
	protected ModelAndView handleRequestInternal(HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		
		Integer examID = Integer.parseInt(request.getParameter("examID"));
		Map model = new HashMap();
		
		Exam exam = examService.gainExam(examID);
		Paper paper = examService.gainPaper(examID);
		
		List<Panduan> panduanList = new ArrayList<Panduan>();
		List<Xuanze> xuanzeList = new ArrayList<Xuanze>();
		List<Tiankong> tiankongList = new ArrayList<Tiankong>();
		
		//准备基础信息
		String subjectName = questionService.gainSubject(exam.getSubject()).getName();
		String teacherName = teacherService.gainTeacher(paper.getTeacher()).getTruename();
		
		//准备判断题
		Integer panduan_num = paper.getPanduan_num();
		String panduan_list_str = paper.getPanduan_list();
		String[] panduanID_array = panduan_list_str.split(",");
		Panduan panduan = null;
		for(int i=0; i<panduan_num; i++){
			panduan = questionService.gainPanduan(Integer.parseInt(panduanID_array[i]));
			panduanList.add(panduan);
		}
		
		//准备选择题
		Integer xuanze_num = paper.getXuanze_num();
		String xuanze_list_str = paper.getXuanze_list();
		String[] xuanzeID_array = xuanze_list_str.split(",");
		Xuanze xuanze = null;
		String[] orderArray = {"owrc","owcr","orwc","orcw",
							   "ocwr","ocrw","worc","wocr",
							   "wroc","wrco","wcor","wcro",
							   "rowc","rocw","rwoc","rwco",
							   "rcow","rcwo","cowr","corw",
							   "cwor","cwro","crow","crwo"};
		for(int i=0; i<xuanze_num; i++){
			xuanze = questionService.gainXuanze(Integer.parseInt(xuanzeID_array[i]));
			//打乱选项的顺序
			String order = orderArray[(int)((orderArray.length)*Math.random())];
			xuanze.setAnswer_order(order);
			for(int j=0; j<order.length(); j++){
				String temp = null;
				//根据随机数提取出一个选项.
				switch(order.charAt(j)){
				case 'c':
					temp = xuanze.getCorrect_answer();
					xuanze.setRight_answer_pos(j);
					break;
				case 'o':
					temp = xuanze.getFalse_one();
					break;
				case 'w':
					temp = xuanze.getFalse_two();
					break;
				case 'r':
					temp = xuanze.getFalse_three();
					break;
				}
				
				//将提取出的选项依次放进ABCD中.
				switch(j){
				case 0:
					xuanze.setAnswer_a(temp);
					break;
				case 1:
					xuanze.setAnswer_b(temp);
					break;
				case 2:
					xuanze.setAnswer_c(temp);
					break;
				case 3:
					xuanze.setAnswer_d(temp);
					break;
				}
			}
			
			xuanzeList.add(xuanze);
		}
		
		//准备填空题
		Integer tiankong_num = paper.getTiankong_num();
		String tiankong_list_str = paper.getTiankong_list();
		String[] tiankongID_array = tiankong_list_str.split(",");
		Tiankong tiankong = null;
		for(int i=0; i<tiankong_num; i++){
			tiankong = questionService.gainTiankong(Integer.parseInt(tiankongID_array[i]));
			tiankongList.add(tiankong);
		}
		
		model.put("paper", paper);
		model.put("subjectName", subjectName);
		model.put("teacherName", teacherName);
		model.put("panduanList", panduanList);
		model.put("xuanzeList", xuanzeList);
		model.put("tiankongList", tiankongList);
		return new ModelAndView("teacher/showPaper", model);
	}

}
